/**
 * Labox���޹�˾��ĿInnovationServer
 * Labox��Ȩ���� @copyright 2009
 * @javaclass LoginPackerHandler.java @author ���� @time 2009-11-27
 */
package labox.innovation.pay.http;

import java.net.InetSocketAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import labox.innovation.config.Config;
import labox.innovation.db.PropGrantDAO;
import labox.innovation.gameserver.datatables.CharNameTable;
import labox.innovation.gameserver.model.FuzeWorld;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.model.itemcontainer.PcInventory;
import labox.innovation.gameserver.network.serverpackets.GUIList;
import labox.innovation.pay.DepositConfig;
import labox.innovation.pay.PayCard;
import com.sun.net.httpserver.HttpExchange;

/**
 * @author Administrator ��ֵϵͳ ���ճ�ֵƽ̨����
 */
public class RechargePackerHandler extends IPacketHandler {

	private static final Logger _log = LoggerFactory.getLogger(RechargePackerHandler.class.getName());
	private static final String RECHARGEGUI = "<form nm='exit' w='210' h='170' tl='��ֵϵͳ'> " + "@@ <br/>" + "�������������������ֵʧ������ѯǬ���ͷ���021-32504360,���Ǿ��������," + "��л���Ǭ����֧�֡�" + "</form>";

	@Override
	public boolean accept(InetSocketAddress address) {
		return true;
	}

	@Override
	public void run(HttpExchange t) {
		try {
			String path = t.getRequestURI().getPath();
			String query = t.getRequestURI().getQuery();// /chargeSystem
			_log.info("��ֵ��������URL:" + t.getRequestURI().toString(), Config.SERVER_ID);
			FuzePcInstance player = null;
			int _change; // ��Ǯת��ϵ��
			PayCard payCard; // �㿨
			t.sendResponseHeaders(200, 0);// ��������OK
			if (path != null && path.equals("/chargeSystem") && query != null) {
				String[] data = query.split("&");
				if (data != null && data.length == 6) {// ��֤URL
					int remark_ = data[0].indexOf("remark=");
					int money_ = data[1].indexOf("money=");
					int orderId_ = data[2].indexOf("orderId=");
					int rechargeableType_ = data[3].indexOf("rechargeableType=");
					int isOk_ = data[4].indexOf("isOk=");
					int cardStatus_ = data[5].indexOf("cardStatus=");
					if (remark_ >= 0 && remark_ + 7 < data[0].length() && money_ >= 0 && money_ + 6 < data[1].length() && orderId_ >= 0 && orderId_ + 8 < data[2].length() && rechargeableType_ >= 0 && rechargeableType_ + 17 < data[3].length() && isOk_ >= 0 && isOk_ + 5 < data[4].length() && cardStatus_ >= 0 && cardStatus_ + 11 < data[5].length()) {
						String remark = data[0].substring(remark_ + 7);
						String money = data[1].substring(money_ + 6);
						String orderId = data[2].substring(orderId_ + 8);
						String rechargeableType = data[3].substring(rechargeableType_ + 17);
						String isOk = data[4].substring(isOk_ + 5);
						String cardStatus = data[5].substring(cardStatus_ + 11);
						String[] remarks = remark.split("-");
						String[] rechargeableTypes = rechargeableType.split("-");
						String server_id = remarks[0];// �˺�����
						long object_id = Long.parseLong(remarks[1]);// ��ȡ�û�object_id
						player = FuzeWorld.getInstance().getPlayer(object_id);// ���������ҵ����
						if (isOk.equals("1")) {// ��ֵƽ̨��Ӧ�ǳɹ�
							String type = rechargeableTypes[0]; // ��������
							String qudao = rechargeableTypes[1];// skyPay ��Ϸ���
							if (qudao != null && qudao.equals(DepositConfig.GAME_NAME)) {// ƥ����Ϸ����
								payCard = DepositConfig.getPAYCARDS(type);
								if (payCard != null) {// �ҵ�ƥ��Ŀ���
									_change = payCard.getChange();
									int _yuanbao = (int) (Double.parseDouble(money) * _change);// ����Ԫ������
									if (player != null) {// ���߳�ֵ
										player.addMoneyYuanBao("��ֵƽ̨����", _yuanbao, null, true);
										_log.info("�û����߳�ֵ�ɹ� :�û��˺�=" + player.getAccountName() + "�û���ɫ��=" + player.getName() + " ��ֵ����Ԫ��=" + _yuanbao + " ��ֵ����=" + type + " ������=" + orderId + " ��ֵϵ��=" + _change + "," + " ��ֵ���=" + money + "", player.getObjectId(), player.getName());
										player.sendPacket(new GUIList(RECHARGEGUI.replace("@@", "��ĳ�ֵ�ѳɹ�,��ֵ���Ϊ " + money + " ,����Ԫ��Ϊ " + _yuanbao + " ��ע�����...")));
									} else {// ��Ҳ�����Ϸ����
										PropGrantDAO.GrantProp(object_id, PcInventory.MONEY_YAUNBAO_ID, _yuanbao);
										_log.info("�û������߳�ֵ�ɹ� :�û��˺�=" + server_id + "��ֵ����Ԫ��=" + _yuanbao + " ��ֵ����=" + type + " ������=" + orderId + " ��ֵϵ��=" + _change + "," + " ��ֵ���=" + money + "", object_id, CharNameTable.getInstance().getNameById(object_id));
									}
								} else {// û���ҵ�ƥ��Ŀ���
									if (player != null) {
										player.sendPacket(new GUIList(RECHARGEGUI.replace("@@", "�Բ���,���ֵ�������Ͳ�ƥ��,�����³�ֵ")));
									}
									_log.info("��ֵʧ��:��ֵ�������Ͳ�ƥ��  " + t.getRequestURI().toString(), object_id);
								}
							} else {// ��Ϸ���Ͳ�ƥ��
								player.sendPacket(new GUIList(RECHARGEGUI.replace("@@", "�Բ���,�����Ϸ�������Ͳ�ƥ��,�����³�ֵ")));
								_log.info("��ֵʧ��:��Ϸ���Ͳ�ƥ�� " + t.getRequestURI().toString(), object_id);
							}

						} else {// ��ֵƽ̨��Ӧ�ǲ��ɹ�
							if (player != null) {
								player.sendPacket(new GUIList(RECHARGEGUI.replace("@@", "�Բ���,��Ŀ�����֤��Ч,������Ŀ������������³�ֵ")));
							}
							_log.info("��ֵʧ��:��ֵƽ̨��Ӧʧ�� �û��˺�=" + server_id + " ������=" + orderId + " ��ֵ���=" + money + "", object_id, player.getName());
						}
					}
				}
			}
		} catch (Exception e) {
			_log.error("��ֵ�����쳣:��������URL��֤����" + t.getRequestURI().toString(), Config.SERVER_ID, e);
		}

	}
}
